#include "diskio.h"
#include "print.h"
#include <driver.h>

static acoral_id  fs_id=ACORAL_DEV_ERR_ID;
/*-----------------------------------------------------------------------*/
/* Initialize Disk Drive                                                 */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
        BYTE drv		/* Physical drive nmuber (0) */
        )
{
	//SD卡的初始化在系统中
	//判断是否打开打开了设备
	acoral_id dev_id;
	if(fs_id != ACORAL_DEV_ERR_ID)
		return 0;
	dev_id = acoral_dev_open("disk");
	if (dev_id == ACORAL_DEV_ERR_ID)
	{
		acoral_prints("\ndisk: error.\n");
		return 1;
	}
	fs_id = dev_id;
	return 0;
}

/*-----------------------------------------------------------------------*/
/* Get Disk Status                                                       */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
        BYTE drv		/* Physical drive nmuber (0) */
        )
{
    if (drv) return STA_NOINIT;		/* Supports only single drive */
    return 0;
}

/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
        BYTE drv,			/* Physical drive nmuber (0) */
        BYTE *buff,			/* Pointer to the data buffer to store read data */
        DWORD sector,		        /* Start sector number (LBA) */
        BYTE count			/* Sector count (1..255) */
        )
{
	int ret;
	acoral_u8* tp;
    ret= acoral_dev_read(fs_id, (void *)buff, count*512, sector, 0);
	return ret ? RES_ERROR : RES_OK;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if _READONLY == 0
DRESULT disk_write (
        BYTE drv,			/* Physical drive nmuber (0) */
        const BYTE *buff,	/* Pointer to the data to be written */
        DWORD sector,		/* Start sector number (LBA) */
        BYTE count			/* Sector count (1..255) */
        )
{
	int ret;
	ret = acoral_dev_write(fs_id, (void*)buff, count*512, sector, 0);
    return ret ? RES_ERROR : RES_OK;
}
#endif /* _READONLY == 0 */



/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

#if _USE_IOCTL != 0
DRESULT disk_ioctl (
        BYTE drv,		/* Physical drive nmuber (0) */
        BYTE ctrl,		/* Control code */
        void *buff		/* Buffer to send/receive control data */
        )
{
	return 0;
}
#endif /* _USE_IOCTL != 0 */

DWORD get_fattime (void)
{
    return 0;  
}
